﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.Service.Shared;
using Bymed.SpineScan.WPF.Model;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;
using System.Windows.Threading;

namespace Bymed.SpineScan.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class SingleTestHistoryDataViewModel : MyViewModelBase
    {
        protected SingleTestItemViewModel CurrentTestItem;

        /// <summary>
        /// Initializes a new instance of the SingleTestDetailViewModel class.
        /// </summary>
        public SingleTestHistoryDataViewModel(IDataService dataService)
            : base(dataService)
        {
            // MUST initialize these two properties here. Otherwize the below exception will occur.
            // 'Cannot modify the logical children for this node at this time because a tree walk is in progress.'
            SelectedTestItem = new SingleTestItemViewModel(null);
            CurrentTestItem = new SingleTestItemViewModel(null);
        }

        /// <summary>
        /// Load history data for data binding.
        /// </summary>
        public virtual void LoadHistoryData(SingleTestItemViewModel testItem)
        {
            IsCustomizedTestItem = testItem.IsCustomizedTestItem;

            // CANNOT change these two properties value directly. Otherwize the below exception will occur.
            // 'Cannot modify the logical children for this node at this time because a tree walk is in progress.'
            ViewModelHelper.CopySingleTestItem(testItem, SelectedTestItem);
            ViewModelHelper.CopySingleTestItem(testItem, CurrentTestItem);

            #region Trick Error
            // Where method call is from TestSummaryPage, then these two lines of code caused exception of 
            // 'Cannot modify the logical children for this node at this time because a tree walk is in progress.'

            //SelectedTestItem = testItem;
            //CurrentTestItem = testItem;
            #endregion
        }

        /// <summary>
        /// The <see cref="IsCustomizedTestItem" /> property's name.
        /// </summary>
        public const string IsCustomizedTestItemPropertyName = "IsCustomizedTestItem";

        private bool _isCustomizedTestItem = false;

        /// <summary>
        /// Sets and gets the IsCustomizedTestItem property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsCustomizedTestItem
        {
            get
            {
                return _isCustomizedTestItem;
            }

            set
            {
                if (_isCustomizedTestItem == value)
                {
                    return;
                }

                RaisePropertyChanging(IsCustomizedTestItemPropertyName);
                _isCustomizedTestItem = value;
                RaisePropertyChanged(IsCustomizedTestItemPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="PatientBasicInfoDisplayViewModel" /> property's name.
        /// </summary>
        public const string PatientBasicInfoDisplayViewModelPropertyName = "PatientBasicInfoDisplayViewModel";

        private PatientBasicInfoDisplayViewModel _patientBasicInfoDisplayViewModel;

        /// <summary>
        /// Sets and gets the PatientBasicInfoDisplayViewModel property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PatientBasicInfoDisplayViewModel PatientBasicInfoDisplayViewModel
        {
            get
            {
                return _patientBasicInfoDisplayViewModel;
            }

            set
            {
                if (_patientBasicInfoDisplayViewModel == value)
                {
                    return;
                }

                RaisePropertyChanging(PatientBasicInfoDisplayViewModelPropertyName);
                _patientBasicInfoDisplayViewModel = value;
                RaisePropertyChanged(PatientBasicInfoDisplayViewModelPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SelectedTestItem" /> property's name.
        /// </summary>
        public const string SelectedTestItemPropertyName = "SelectedTestItem";

        private SingleTestItemViewModel _selectedTestItem = null;

        /// <summary>
        /// Sets and gets the SelectedTestItem property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public SingleTestItemViewModel SelectedTestItem
        {
            get
            {
                return _selectedTestItem;
            }

            set
            {
                if (_selectedTestItem == value)
                {
                    return;
                }

                if (value == null)
                {
                    value = CurrentTestItem;
                }

                RaisePropertyChanging(SelectedTestItemPropertyName);
                _selectedTestItem = value;
                RaisePropertyChanged(SelectedTestItemPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="HistoryAnalysisChartSource" /> property's name.
        /// </summary>
        public const string HistoryAnalysisChartSourcePropertyName = "HistoryAnalysisChartSource";

        private ObservableCollection<SingleTestItemViewModel> _historyAnalysisChartSource = new ObservableCollection<SingleTestItemViewModel>();

        /// <summary>
        /// Sets and gets the HistoryAnalysisChartSource property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<SingleTestItemViewModel> HistoryAnalysisChartSource
        {
            get
            {
                return _historyAnalysisChartSource;
            }

            set
            {
                if (_historyAnalysisChartSource == value)
                {
                    return;
                }

                RaisePropertyChanging(HistoryAnalysisChartSourcePropertyName);
                _historyAnalysisChartSource = value;
                RaisePropertyChanged(HistoryAnalysisChartSourcePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="HistoryDataList" /> property's name.
        /// </summary>
        public const string HistoryDataListPropertyName = "HistoryDataList";

        private ObservableCollection<SingleTestItemViewModel> _historyDataList = new ObservableCollection<SingleTestItemViewModel>();

        /// <summary>
        /// Sets and gets the HistoryDataList property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<SingleTestItemViewModel> HistoryDataList
        {
            get
            {
                return _historyDataList;
            }

            set
            {
                if (_historyDataList == value)
                {
                    return;
                }

                RaisePropertyChanging(HistoryDataListPropertyName);
                _historyDataList = value;
                RaisePropertyChanged(HistoryDataListPropertyName);
            }
        }
    }
}
